package org.ydsw.data.manage.controller;


import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import org.ydsw.data.manage.config.ClickhouseConfig;
import org.ydsw.data.manage.domain.HotDataDomain;
import org.ydsw.data.manage.domain.XyObj;
import org.ydsw.data.manage.entity.FirstPageContent;
import org.ydsw.data.manage.entity.UserInfoLoginTest;
import org.ydsw.data.manage.utils.JsonResponse;

import javax.annotation.Resource;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


@Slf4j
@RestController
@RequestMapping("/api")
public class UserLoginController {

    @PostMapping("/user/info")
    public JsonResponse loginInfo() {
        UserInfoLoginTest userInfoLoginTest = new UserInfoLoginTest();
        userInfoLoginTest.setName("钟山峰");
        userInfoLoginTest.setRole("钟山峰");
        userInfoLoginTest.setCertification(1);
        userInfoLoginTest.setRole("admin");

        return JsonResponse.respSuccess(userInfoLoginTest);
    }

    @GetMapping("/content-data")
    public JsonResponse contentData() {
        ArrayList<XyObj> xyObjs = new ArrayList<>();
        xyObjs.add(new XyObj("2024-01-05", 58));
        xyObjs.add(new XyObj("2024-01-06", 81));
        xyObjs.add(new XyObj("2024-01-07", 43));
        xyObjs.add(new XyObj("2024-01-08", 97));
        xyObjs.add(new XyObj("2024-01-09", 74));
        xyObjs.add(new XyObj("2024-01-10", 88));
        xyObjs.add(new XyObj("2024-01-11", 49));
        xyObjs.add(new XyObj("2024-01-12", 79));

        return JsonResponse.respSuccess(xyObjs);
    }


    @GetMapping("/popular/list")
    public JsonResponse popularData() {
        ArrayList<HotDataDomain> hotDataDomains = new ArrayList<>();
        hotDataDomains.add(new HotDataDomain(1, "200W+", "CRM销售流向管理...", 22));
        hotDataDomains.add(new HotDataDomain(2, "180W+", "ERP供应链...", 80));
        hotDataDomains.add(new HotDataDomain(3, "150W+", "ERP月度资金计划管理..", 62));
        hotDataDomains.add(new HotDataDomain(4, "100W+", "CRM前端销售行为管理...", 12));
        hotDataDomains.add(new HotDataDomain(5, "10W+", "DMP数据管理...", 99));

        return JsonResponse.respSuccess(hotDataDomains);
    }


    @Resource
    ClickhouseConfig clickhouseConfig;
    private static Connection conn = null;
    private static PreparedStatement ps = null;

    @GetMapping("/getContentData")
    public JsonResponse getContentData() {
        String totalCount = "";
        String str = "yuandong_ods.scm_erp_ods";
        try {
            conn = clickhouseConfig.getChClient();
            String partitionSql = "SELECT COUNT(1) as totalCount from " + str;
            //查询total count
            ps = conn.prepareStatement(partitionSql);
            ResultSet resultCount = ps.executeQuery();
            while (resultCount.next()) {
                totalCount = resultCount.getString("totalCount");
            }
            System.out.println(totalCount);

        } catch (SQLException | ClassNotFoundException e1) {
            e1.printStackTrace();
        }
        FirstPageContent firstPageContent = new FirstPageContent();
        firstPageContent.setOnlineContent(Integer.parseInt(totalCount));
        firstPageContent.setPutIn(10);
        firstPageContent.setNewDay((int) (firstPageContent.getOnlineContent() * 0.01 * 3));
        firstPageContent.setNewFromYesterday(3);
        return JsonResponse.respSuccess(firstPageContent);
    }


}
